package serializable.fst;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;
import org.nustaq.serialization.FSTConfiguration;
import org.nustaq.serialization.FSTObjectInput;

public class FstDemo {
	
	public void writeObj(OutputStream outputStream,Object obj) throws IOException {
		FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();
		conf.getObjectOutput(outputStream).writeObject(obj,obj.getClass());
		outputStream.flush();
		outputStream.close();
	}
	
	@SuppressWarnings("unchecked")
	public <T> T readObj(InputStream inputStream,Class<T> tagretC) throws Exception {
		FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();
		FSTObjectInput input = conf.getObjectInput(inputStream);
		return (T) input.readObject();
	}
	
	@Test
	public void writeFile() {
		A a = new A();
		a.setA("a");
		try {
			File f = new File("a.fst");
			if(!f.exists()) {
				f.createNewFile();
			}
			writeObj(new FileOutputStream(f),a);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void readObj() {
		try {
			FileInputStream fis = new FileInputStream("a.fst");
			A a = readObj(fis, A.class);
			System.out.println(a);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
